$(document).ready(function() {
    $("[name=username]").click(function() {
		$("#sign_in_alert").hide()
	});
	$("[name=password]").click(function() {
		$("#sign_in_alert").hide()
	});
	$("#sign_in_submit").bind("click", function() {
		var c = $("[name=username]").val();
		var d = $("[name=password]").val();
		var e = $(this);
		if (c == "" || d == "") {
			$("#sign_in_alert").html("Username Or Password Can not be Empty.");
			$("#sign_in_alert").show();
			return
		}
		$.ajax({
			url: "/sign-in/",
			type: "post",
			dataType: "json",
			data: {
				"username": c,
				"password": d,
				"csrfmiddlewaretoken": $("[name=csrfmiddlewaretoken]").val(),
				"next": $("[name=next]").val()
			},
			success: function(a) {
				var b = a["code"];
				if (b != "200") {
					$("#sign_in_alert").html(a["message"]);
					$("#sign_in_alert").show();
					return
				}
				location.href = $("[name=next]").val()
			},
			error: function(a) {
				$("sign_in_alert").show()
			},
			statusCode: {
				404: function() {
					("#sign_in_alert").val("404 not found");
					$("#sign_in_alert").show()
				},
				400: function() {
					alert("bad request");
					$("#sign_in_alert").show()
				}
			}
		})
	})
});
$(document).ready(function() {
	$("[name=username]").click(function() {
		$("#sign_up_alert").hide()
	});
	$("[name=password]").click(function() {
		$("#sign_up_alert").hide()
	});
	$("[name=dopassword]").click(function() {
		$("#sign_up_alert").hide()
	});
	$("#sign_up_submit").bind("click", function() {
		var b = $(this);
		var c = $("[name=sign_up_username]").val();
		var d = $("[name=sign_up_password]").val();
		var e = $("[name=sign_up_dopassword]").val();
		if (c == "" || d == "" || e == "") {
			$("#sign_up_alert").html("Username Or Password Can not be Empty.");
			$("#sign_up_alert").show();
			return
		}
		if (d != e) {
			$("#sign_up_alert").html("Passwords not the same.");
			$("#sign_up_alert").show();
			return
		}
		$.ajax({
			url: "/sign-up/",
			type: "post",
			dataType: "json",
			data: {
				"username": c,
				"password": d,
				"dopassword": e,
				"csrfmiddlewaretoken": $("[name=csrfmiddlewaretoken]").val(),
				"next": $("[name=next]").val()
			},
			success: function(a) {
				location.href = $("[name=next]").val()
			},
			error: function(a) {
				alert(a["code"])
			},
			statusCode: {
				404: function() {
					alert("page not found")
				},
				400: function() {
					alert("bad request")
				}
			}
		})
	})
});
$(document).ready(function() {
	$("#send-message-submit").bind("click", function() {
		send_message_count += 1;
		var b = $("[name=from-who]").val();
		var c = $("[name=from-tel]").val();
		var d = $("[name=to-who]").val();
		var e = $("[name=to-tel]").val();
		var f = $("[name=message-content]").val();
		var g = $("[name=send-date]").val();
		var h = {
			csrfmiddlewaretoken: $("[name=csrfmiddlewaretoken]").val(),
			'from_who': b,
			'from_phone': c,
			'to_who': d,
			'to_phone': e,
			'content': f,
			'send_date': g
		}
		$.ajax({
			url: "/message/",
			type: "post",
			dateType: "json",
			data: h,
			success: function(a) {
				$('#send-message-panel').modal('hide')
			},
			error: function() {
				alert("error send-message-submit")
			}
		})
	})
});
$(document).ready(function() {
	$("#send-date").on("input", function() {
		// alert("on change")
	})
});